<?php

/**
 * 默认控制器
 */
class Controller_Adminhtmltemplates extends Controller_Abstract
{
    function actionIndex()
    {
    	if(isset($_GET['ope'])){
    		if($_GET['ope'] == 'delete'){
    			$id = $this->_context->get('id', null);
    			if($id){
    				Htmltemplates::model()->delete(array('id' => $id));
    			}
    		}
    	}
    	
    	$htmlTemplatesAdded = Htmltemplates::model()->getAll();
    	$htmlTemplatesAdded = Htmltemplates::model()->setResultKey($htmlTemplatesAdded, 'root_foldername');
    	
    	$this->_view['htmlTemplatesAdded'] = $htmlTemplatesAdded;
    	
    	$this->_view['areaTypes'] = Htmltemplatestypearea::model()->find();
    	$this->_view['colorTypes'] = Htmltemplatestypecolor::model()->find();
    	$this->_view['columnwidthTypes'] = Htmltemplatestypecolumnwidth::model()->find();
    	$this->_view['sidebarcornerTypes'] = Htmltemplatestypesidebarcorner::model()->find();
    	$this->_view['usingTypes'] = Htmltemplatestypeusing::model()->find();
    	
    	$this->_viewname = '../admin/htmltamplates/index';
    }
    
	function actionSearchnew()
	{
    	$htmlTemplateModel = new Htmltemplates();
    	
    	$addForm = $this->_context->post('form', null);
    	if($addForm){
    		$time = time();
    		$addForm['created_date'] = date('Y-m-d H:i:s', $time);
    		$addForm['created_int'] = $time;
    		$addForm['view_num'] = 0;
    		$addForm['download_num'] = 0;
    		$htmlTemplateModel->attributes = array_merge($htmlTemplateModel->attributes, $addForm);
	   		$this->_returnAjax(
    			array(
    				'result' => $htmlTemplateModel->save(true),
    				'errorinfo' => $htmlTemplateModel->getValidateError()
    			)		
    		);
    	}
    	
    	$htmlTemplatesUnAdded = $htmlTemplateModel->getUnaddedTemplates();
    	
    	$this->_view['htmlTemplatesUnAdded'] = $htmlTemplatesUnAdded;	
    	$this->_view['htmlTemplateModel'] = $htmlTemplateModel;
    	$this->_view['areaTypes'] = Htmltemplatestypearea::model()->find();
    	$this->_view['colorTypes'] = Htmltemplatestypecolor::model()->find();
    	$this->_view['columnwidthTypes'] = Htmltemplatestypecolumnwidth::model()->find();
    	$this->_view['sidebarcornerTypes'] = Htmltemplatestypesidebarcorner::model()->find();
    	$this->_view['usingTypes'] = Htmltemplatestypeusing::model()->find();

    	$this->_viewname = '../admin/htmltamplates/searchnew';
	}
	
	function actionGetdiskinfo()
	{
		$id = $this->_context->get('id', null);
		if($id){
			$ht = Htmltemplates::model()->find('*', array('id' => $id), null, null, array('onlyOne' => true));
			
			$htDisk = null;
			if(!empty($ht['root_foldername'])){
				$htDisk = Htmltemplates::model()->getTemplateOnDiskByRootfolder($ht['root_foldername']);
			}
			$this->_returnAjax(array(
				'infoDisk' => $htDisk,
				'infoSaved' => $ht
			));
		}
		
	}	
	
	function actionEdit()
	{
    	$htmlTemplateModel = new Htmltemplates();
    	
	    $editForm = $this->_context->post('form', null);
    	if($editForm){
    		$htmlTemplateModel = new Htmltemplates();
    		$htmlTemplateModel->attributes = $editForm;
    		if(!$htmlTemplateModel->validate()){
				$this->_returnAjax(array(
    				'result' => false,
    				'errorinfo' => $htmlTemplateModel->getValidateError()
	    		));
			}
	   		$this->_returnAjax(array(
    			'result' => $htmlTemplateModel->save(true),
    			'errorinfo' => $htmlTemplateModel->getValidateError()
    		));
    	}
    	
    	$htmlTemplatesUnAdded = $htmlTemplateModel->getUnaddedTemplates();
    	
    	$this->_view['htmlTemplatesUnAdded'] = $htmlTemplatesUnAdded;	
    	$this->_view['htmlTemplateModel'] = $htmlTemplateModel;
    	$this->_view['areaTypes'] = Htmltemplatestypearea::model()->find();
    	$this->_view['colorTypes'] = Htmltemplatestypecolor::model()->find();
    	$this->_view['columnwidthTypes'] = Htmltemplatestypecolumnwidth::model()->find();
    	$this->_view['sidebarcornerTypes'] = Htmltemplatestypesidebarcorner::model()->find();
    	$this->_view['usingTypes'] = Htmltemplatestypeusing::model()->find();

    	$this->_viewname = '../admin/htmltamplates/searchnew';
	}
	
	function actionType()
	{
		$typeAreaModel = new Htmltemplatestypearea();
		$typeColorModel = new Htmltemplatestypecolor();
		$typeColumnWidthModel = new Htmltemplatestypecolumnwidth();
		$typeSidebarCornerModel = new Htmltemplatestypesidebarcorner();
		$typeUsingModel = new Htmltemplatestypeusing();
		$whichType = $this->_context->get('whichType', null);
		
		if(isset($_GET['ope'])){
			if($_GET['ope'] == 'add'){
				$whichType = $this->_context->post('whichType', null);
				if($whichType){
					$attributes = array();
					$attributes['name'] = $this->_context->post('name', '');
					$attributes['name_en'] = $this->_context->post('name_en', '');
					switch($whichType){
						case 'area':
							$typeAreaModel->attributes = $attributes;
							$typeAreaModel->save();
							break;
						case 'color':
							$attributes['color_hex'] = $this->_context->post('color_hex', '');
							$typeColorModel->attributes = $attributes;
							$typeColorModel->save();
							break;
						case 'columnwidth':
							$typeColumnWidthModel->attributes = $attributes;
							$typeColumnWidthModel->save();
							break;
						case 'sidebarcorner':
							$typeSidebarCornerModel->attributes = $attributes;
							$typeSidebarCornerModel->save();
							break;
						case 'using':
							$typeUsingModel->attributes = $attributes;
							$typeUsingModel->save();
					}
				}
			}elseif($_GET['ope'] == 'delete'){
				$id = $this->_context->get('id', null);
				if($id && $whichType){
					switch($whichType){
						case 'area':
							$typeAreaModel->delete(array('id' => $id));
							break;
						case 'color':
							$typeColorModel->delete(array('id' => $id));
							break;
						case 'columnwidth':
							$typeColumnWidthModel->delete(array('id' => $id));
							break;
						case 'sidebarcorner':
							$typeSidebarCornerModel->delete(array('id' => $id));
							break;
						case 'using':
							$typeUsingModel->delete(array('id' => $id));
					}
				}
			}elseif($_GET['ope'] == 'setdefault'){
				$id = $this->_context->get('id', null);
				if($id && $whichType){
					switch($whichType){
						case 'area':
							$typeAreaModel->setDefaultById($id);
							break;
						case 'color':
							$typeColorModel->setDefaultById($id);
							break;
						case 'columnwidth':
							$typeColumnWidthModel->setDefaultById($id);
							break;
						case 'sidebarcorner':
							$typeSidebarCornerModel->setDefaultById($id);
							break;
						case 'using':
							$typeUsingModel->setDefaultById($id);
					}
				}
			}
			
			return $this->_redirect(url('adminhtmltemplates/type', array('whichType' => $whichType)));
		}
		
		$this->_view['whichType'] = isset($whichType)? $whichType : '';
		
		$this->_view['typeAreas'] = $typeAreaModel->find();
		$this->_view['typeColors'] = $typeColorModel->find();
		$this->_view['typeColumnWidths'] = $typeColumnWidthModel->find();
		$this->_view['typeSidebarCorners'] = $typeSidebarCornerModel->find();
		$this->_view['typeUsings'] = $typeUsingModel->find();
		
		$this->_view['typeAreaModel'] = $typeAreaModel;
		$this->_view['typeColorModel'] = $typeColorModel;
		$this->_view['typeColumnWidthModel'] = $typeColumnWidthModel;
		$this->_view['typeSidebarCornerModel'] = $typeSidebarCornerModel;
		$this->_view['typeUsingModel'] = $typeUsingModel;
		$this->_viewname = '../admin/htmltamplates/type';
	}
	
	
	
	
	
}

